package com.bangbang.controller;


import com.bangbang.configresult.Result;
import com.bangbang.configresult.ResultEnum;
import com.bangbang.dto.UserDTO;
import com.bangbang.feign.UserServiceFeign;
import com.bangbang.pojo.Order;
import com.bangbang.service.OrderService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("testOrder")
public class TestOrderController {


    @Autowired
    private OrderService orderService;

    @Autowired
    private UserServiceFeign userServiceFeign;


    // 增
    @ApiOperation(value = "订单增加方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "order",value = "订单的实体类",required = true,paramType = "body"),
            @ApiImplicitParam(name = "TestUserId",value = "用户Id",required = true,paramType = "query")
    })
    @PostMapping
    public Result save(@RequestBody Order order, @RequestParam Integer TestUserId){
        Result<UserDTO> result = userServiceFeign.selectById(TestUserId);
        if (result.getCode().intValue() == 1){
            UserDTO userDTO = result.getEntity();
            order.setAddr(userDTO.getAddr());
            orderService.save(order);
            return Result.buildResult(ResultEnum.SUCCESS);
        }
            return Result.buildResult(ResultEnum.SUCCESS).setEntity(result.getEntity());
    }

    //  删
    @ApiOperation(value = "订单删除方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId",value = "订单的id",required = true,paramType = "path"),
    })
    @DeleteMapping("/{orderId}")
    public Result delete(@PathVariable("orderId") Integer orderId){
        orderService.delete(orderId);
        return Result.buildResult(ResultEnum.SUCCESS);
    }

    // 改
    @ApiOperation(value = "订单修改方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "order",value = "订单的实体类",required = true,paramType = "body"),
    })
    @PutMapping
    public Result update(Order order){
        orderService.update(order);
        return Result.buildResult(ResultEnum.SUCCESS);
    }
    //  查
    @ApiOperation(value = "订单查询方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId",value = "订单的id",required = true,paramType = "path"),
    })
    @GetMapping("/{orderId}")
    public Result selectById(@PathVariable("orderId") Integer orderId){
        Order order = orderService.selectById(orderId);
        return Result.buildResult(ResultEnum.SUCCESS).setResult(order);
    }


}
